package com.gx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gx.common.result.AppResult;
import com.gx.model.dto.task.FlowTaskCompleteDto;
import com.gx.model.dto.task.FlowTaskReturnDto;
import com.gx.model.dto.task.TaskQueryDto;
import com.gx.model.vo.FlowTaskVo;
import com.gx.model.vo.MyHisTaskVo;
import com.gx.model.vo.ReturnTaskVo;
import com.gx.service.FlowTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName FlowTaskController
 * @Author lpj
 * @Date 2022/12/22 14:46
 **/
@RestController
@RequestMapping("/flowable/task")
@Api(tags = "任务管理")
public class FlowTaskController {
    @Autowired
    private FlowTaskService flowTaskService;


    @ApiOperation(value = "发起列表")
    @PostMapping(value = "/myProcess")
    public AppResult<Page<FlowTaskVo>> myProcess(@RequestBody TaskQueryDto dto) {
        Page<FlowTaskVo> page = flowTaskService.myProcess(dto);
        return AppResult.success(page);
    }

    @ApiOperation(value = "待办列表")
    @PostMapping(value = "/todoList")
    public AppResult<Page<FlowTaskVo>> todoList(@RequestBody TaskQueryDto taskQueryDto) {
        Page<FlowTaskVo> flowTaskVoPage = flowTaskService.todoList(taskQueryDto);
        return AppResult.success(flowTaskVoPage);

    }

    @ApiOperation(value = "我的已办任务", notes = "针对审批动作,审批一次就是一次记录,一个流程可能回出现多次")
    @PostMapping(value = "/finishedList")
    @ApiIgnore
    public AppResult<Page<FlowTaskVo>> finishedList(@RequestBody TaskQueryDto dto) {
        Page<FlowTaskVo> page = flowTaskService.finishedList(dto);
        return AppResult.success(page);
    }

    @ApiOperation(value = "我已处理流程数据")
    @PostMapping(value = "/myHisTaskList")
    public AppResult<Page<MyHisTaskVo>> myHisTaskList(@RequestBody TaskQueryDto dto) {
        Page<MyHisTaskVo> taskVoPage = flowTaskService.getMyHisTaskList(dto);
        return AppResult.success(taskVoPage);
    }

    @ApiOperation(value = "任务审批")
    @PostMapping(value = "/complete")
    public AppResult<Page<FlowTaskVo>> complete(@Valid @RequestBody FlowTaskCompleteDto completeDto) {
        flowTaskService.complete(completeDto);
        return AppResult.success();
    }

    @ApiOperation(value = "任务回退")
    @PostMapping(value = "/return")
    public AppResult<String> taskReturn(@RequestBody FlowTaskReturnDto returnDto) {
        flowTaskService.taskReturn(returnDto);
        return AppResult.success("退回成功!");
    }

    @ApiOperation(value = "回退节点列表")
    @PostMapping(value = "/returnList")
    public AppResult<List<ReturnTaskVo>> findReturnTaskList(@RequestParam String taskId) {
        List<ReturnTaskVo> returnTaskList = flowTaskService.findReturnTaskList(taskId);
        return AppResult.success(returnTaskList);
    }
}
